//引入redux

import {
  applyMiddleware,
  combineReducers,
  legacy_createStore as createStore,
  compose,
} from "redux";
import TabbarReducer from "./reducers/TabbarReducer";
import CityReducer from "./reducers/CityReducer";
import CinemasListReducer from "./reducers/CinemasListReducer";
import reduxThunk from "redux-thunk";
import reduxPromise from "redux-promise";
// 用于合并多个reducer
const reducer = combineReducers({
  TabbarReducer,
  CityReducer,
  CinemasListReducer,
});
// 创建的时候就要把中间件加上去   applyMiddleware应用中间件
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const store = createStore(
  reducer,
  composeEnhancers(applyMiddleware(reduxThunk, reduxPromise))
);
// const store = createStore(reducer, applyMiddleware(reduxThunk, reduxPromise));
/*
store.dispatch
store.subscrbe
store.getState
*/
//redux原理实现
function createLzqStore(reducer) {
  var list = [];
  var state = reducer(); /*调用是因为reducer初次返回一个默认值*/
  function subscrbe(callback) {
    /*subscrbe就是把回调函数传进来*/
    list.push(callback);
  }

  function dispatch(action) {
    // dispatch把之前的state覆盖掉
    state = reducer(state, action);
    for (var i in list) {
      list[i] && list[i]();
      /*遍历list并且进行调用 */
    }
  }

  function getState() {
    return state;
    // 返回最新的state值
  }
  return {
    dispatch,
    subscrbe,
    getState,
  };
}
export default store;
